﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using Pap.Data;
using Pap.Data.Abstraction.Utils;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Shared.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class UserMessagesViewModel : IViewModel
    {

        /// <summary>
        /// 
        /// </summary>
        private IUserMessagesRepository _userMessagesRepository;
        /// <summary>
        /// 
        /// </summary>
        private IMembershipRepository _MembershipRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="UserMessagesViewModel"/> class.
        /// </summary>
        /// <param name="UserMessagesRepository">The user messages repository.</param>
        /// <param name="MembershipRepository">The membership repository.</param>
        public UserMessagesViewModel(IUserMessagesRepository UserMessagesRepository, IMembershipRepository MembershipRepository)
        {
            _userMessagesRepository = UserMessagesRepository;
            _MembershipRepository = MembershipRepository;
        }

        /// <summary>
        /// Gets the user messages.
        /// </summary>
        /// <param name="UserID">The user ID.</param>
        /// <returns></returns>
        public IEnumerable<MessageDetails> GetUserMessages(Guid UserID)
        {
            return (from message in _userMessagesRepository.FindUserMessages(uM => uM.ToUserID == UserID)
                    orderby message.CreationDate descending
                    select new MessageDetails()
                    {
                        MessageID = message.UserMessageID,
                        From = message.FromUserObj.DisplayName,
                        Subject = message.Subject,
                        SendDate = message.CreationDate.ToString("dd-MM-yyyy"),
                        UserName = message.ToUserObj.DisplayName
                    });
        }

        /// <summary>
        /// Gets the message by ID.
        /// </summary>
        /// <param name="MessageID">The message ID.</param>
        /// <returns></returns>
        public IUserMessage GetMessageByID(Guid MessageID)
        {
            return this._userMessagesRepository.GetUserMessageByID(MessageID);

        }

        /// <summary>
        /// Deletes the user message.
        /// </summary>
        /// <param name="userMessageID">The user message ID.</param>
        public void DeleteUserMessage(Guid userMessageID)
        {
            using (IUnitOfWork transaction = ServiceLocator.Current.GetInstance<IUnitOfWork>())
            {
                _userMessagesRepository.DeleteUserMessages(uM => uM.UserMessageID == userMessageID);
                transaction.Commit();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public class MessageDetails
        {
            /// <summary>
            /// Gets or sets the message ID.
            /// </summary>
            /// <value>
            /// The message ID.
            /// </value>
            public Guid MessageID { get; set; }
            /// <summary>
            /// Gets or sets from.
            /// </summary>
            /// <value>
            /// From.
            /// </value>
            public string From { get; set; }
            /// <summary>
            /// Gets or sets the subject.
            /// </summary>
            /// <value>
            /// The subject.
            /// </value>
            public string Subject { get; set; }
            /// <summary>
            /// Gets or sets the send date.
            /// </summary>
            /// <value>
            /// The send date.
            /// </value>
            public string SendDate { get; set; }
            /// <summary>
            /// Gets or sets the name of the user.
            /// </summary>
            /// <value>
            /// The name of the user.
            /// </value>
            public string UserName { get; set; }
        }


        /// <summary>
        /// Gets the name of the user.
        /// </summary>
        /// <param name="UserID">The user ID.</param>
        /// <returns></returns>
        public string GetUserName(Guid UserID)
        {
            return this._MembershipRepository.GetUserByID(UserID).DisplayName;
        }
    }
}
